﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace NarutoClient
{
    /// <summary>
    /// Trida starajici se o komunikaci klienta.
    /// </summary>
    public class ClientCommunication
    {
        #region Fields

        MainWindow client;
        Socket socket;
        byte[] buffer;
        Thread receiveThread;

        int type;
        MessageLibrary.Messages.Message message;

        #endregion // Fields

        #region Properties

        /// <summary>
        /// Vrati buffer prijatych dat.
        /// </summary>
        public byte[] Buffer { get { return buffer; } }

        /// <summary>
        /// Typ prijate zpravy.
        /// </summary>
        public int Type { get { return type; } }

        /// <summary>
        /// Deserializovana prijata zprava.
        /// </summary>
        public MessageLibrary.Messages.Message Message { get { return message; } }

        #endregion // Properties

        public ClientCommunication(MainWindow client, Socket socket)
        {
            this.client = client;
            this.socket = socket;
        }

        /// <summary>
        /// Prijme zpravu ze serveru.
        /// </summary>
        public void Receive()
        {
            // pred kazdym Receive vyprazdnime buffer
            this.buffer = new byte[1024];

            receiveThread = new Thread(Listen);

            receiveThread.Start();
        }

        /// <summary>
        /// Posloucha v oddelenem threadu na Receive dat ze serveru.
        /// </summary>
        private void Listen()
        {
            try
            {
                socket.Receive(buffer);

                MessageLibrary.MessageComposer.Deserialize(buffer, out type, out message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void ReceiveEnemy()
        {
            buffer = new byte[1024];

            receiveThread = new Thread(ListenEnemy);

            receiveThread.Start();
        }

        private void ListenEnemy()
        {
            try
            {
                socket.Receive(buffer);

                MessageLibrary.MessageComposer.Deserialize(buffer, out type, out message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            client.Dispatcher.Invoke(new Action(ProcessEnemy));
        }

        private void ProcessEnemy()
        {
            client.BattlePage = new Pages.BattlePage(client);
            client.Content = client.BattlePage;
        }

        public void ReceiveState()
        {
            buffer = new byte[1024];

            receiveThread = new Thread(ListenState);

            receiveThread.Start();
        }

        private void ListenState()
        {
            try
            {
                socket.Receive(buffer);

                MessageLibrary.MessageComposer.Deserialize(buffer, out type, out message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            client.Dispatcher.Invoke(new Action(ProcessState));
        }

        private void ProcessState()
        {
            client.BattlePage.actionListBox.IsEnabled = true;
            client.BattlePage.confirmButton.IsEnabled = true;
            client.BattlePage.waitingTextBlock.Visibility = System.Windows.Visibility.Hidden;
            client.BattlePage.Update();
        }

        public void Join()
        {
            this.receiveThread.Join();
        }

        /// <summary>
        /// Posle packet z klienta serveru.
        /// </summary>
        /// <param name="buffer"></param>
        public void Send(byte[] buffer)
        {
            socket.Send(buffer);
        }
    }
}
